package com.example.demo.controller;

import com.example.demo.model.Dishes;
import com.example.demo.service.DishesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dishes")
public class DishesController {
    private final DishesService dishesService;

    @Autowired
    public DishesController(DishesService dishesService) {
        this.dishesService = dishesService;
    }

    @PostMapping("/recognizeFood")
    public Dishes recognizeFood(@RequestBody Map<String, String> imageData) {
        // 调用识别 AI 模型获取食品信息
        // 根据识别结果进行分类并返回食品对象
        String imageBase64 = imageData.get("imageData");
        Dishes recognizedFood = dishesService.recognizeFood(imageBase64);
        return recognizedFood;
    }
    @GetMapping
    public List<Dishes> getAllDishes() {
        return dishesService.getAllDishes();
    }

    @GetMapping("/{id}")
    public Dishes getDishesById(@PathVariable Long id) {
        return dishesService.getDishesById(id);
    }

    @PostMapping
    public Dishes addDishes(@RequestBody Dishes dishes) {
        return dishesService.addDishes(dishes);
    }

    @PostMapping("/view/{dishId}")
    public ResponseEntity<String> viewDish(@PathVariable Long dishId) {
        dishesService.incrementDishViewCount(dishId);
        return ResponseEntity.ok("Dish view count updated successfully");
    }

    // 获取分类后的菜品数据
    @GetMapping("/classified")
    public Map<String, List<Dishes>> getClassifiedDishes() {
        return dishesService.classifyDishesByIngredient();
    }

    // 根据食材名称搜索菜品
    @GetMapping("/search")
    public List<Dishes> searchDishesByIngredient(@RequestParam String ingredientName) {
        return dishesService.getDishesByIngredientName(ingredientName);
    }

    @PostMapping("/RecoommandForUserInfo")
    public List<Dishes> RecommandForUserInfo(@RequestBody String description) {
        // 对用户的自然语言进行处理
        List<Dishes> recommandFood = dishesService.RecommandForUserInfo(description);
        return recommandFood;
    }

}
